// 验证码存储服务
interface VerificationCode {
  code: string;
  email: string;
  timestamp: number;
  attempts: number; // 验证尝试次数
}

// 使用Map存储验证码，key为邮箱地址
const verificationCodes = new Map<string, VerificationCode>();

// 验证码有效期（5分钟）
const CODE_EXPIRY_TIME = 5 * 60 * 1000;

// 最大验证尝试次数
const MAX_ATTEMPTS = 3;

// 生成6位数字验证码
export const generateVerificationCode = (): string => {
  return Math.floor(100000 + Math.random() * 900000).toString();
};

// 存储验证码
export const storeVerificationCode = (email: string, code: string): void => {
  const verificationData: VerificationCode = {
    code,
    email,
    timestamp: Date.now(),
    attempts: 0,
  };
  
  verificationCodes.set(email, verificationData);
  
  // 设置定时清理过期验证码
  setTimeout(() => {
    cleanupExpiredCode(email);
  }, CODE_EXPIRY_TIME);
};

// 验证验证码
export const verifyCode = (email: string, inputCode: string): { success: boolean; message: string } => {
  const storedData = verificationCodes.get(email);
  
  if (!storedData) {
    return {
      success: false,
      message: '验证码不存在或已过期，请重新获取'
    };
  }
  
  // 检查是否过期
  if (Date.now() - storedData.timestamp > CODE_EXPIRY_TIME) {
    verificationCodes.delete(email);
    return {
      success: false,
      message: '验证码已过期，请重新获取'
    };
  }
  
  // 检查尝试次数
  if (storedData.attempts >= MAX_ATTEMPTS) {
    verificationCodes.delete(email);
    return {
      success: false,
      message: '验证码尝试次数过多，请重新获取'
    };
  }
  
  // 验证码匹配
  if (storedData.code === inputCode) {
    verificationCodes.delete(email);
    return {
      success: true,
      message: '验证码验证成功'
    };
  } else {
    // 增加尝试次数
    storedData.attempts++;
    verificationCodes.set(email, storedData);
    
    return {
      success: false,
      message: `验证码错误，还有${MAX_ATTEMPTS - storedData.attempts}次尝试机会`
    };
  }
};

// 清理过期验证码
export const cleanupExpiredCode = (email: string): void => {
  const storedData = verificationCodes.get(email);
  if (storedData && Date.now() - storedData.timestamp > CODE_EXPIRY_TIME) {
    verificationCodes.delete(email);
  }
};

// 检查邮箱是否已有验证码
export const hasVerificationCode = (email: string): boolean => {
  const storedData = verificationCodes.get(email);
  if (!storedData) return false;
  
  // 如果已过期，删除并返回false
  if (Date.now() - storedData.timestamp > CODE_EXPIRY_TIME) {
    verificationCodes.delete(email);
    return false;
  }
  
  return true;
};

// 获取验证码剩余时间（秒）
export const getCodeRemainingTime = (email: string): number => {
  const storedData = verificationCodes.get(email);
  if (!storedData) return 0;
  
  const remaining = CODE_EXPIRY_TIME - (Date.now() - storedData.timestamp);
  return Math.max(0, Math.floor(remaining / 1000));
};

// 清理所有过期验证码（定期清理任务）
export const cleanupAllExpiredCodes = (): void => {
  const now = Date.now();
  for (const [email, data] of verificationCodes.entries()) {
    if (now - data.timestamp > CODE_EXPIRY_TIME) {
      verificationCodes.delete(email);
    }
  }
};

// 启动定期清理任务（每5分钟清理一次）
setInterval(cleanupAllExpiredCodes, 5 * 60 * 1000);
